package com.ims.core.doc.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ims.common.annotation.Log;
import com.ims.common.core.controller.BaseController;
import com.ims.common.core.domain.AjaxResult;
import com.ims.common.enums.BusinessType;
import com.ims.core.doc.domain.TInDeductionStat;
import com.ims.core.doc.service.ITInDeductionStatService;
import com.ims.common.utils.poi.ExcelUtil;
import com.ims.common.core.page.TableDataInfo;

/**
 * 统计表Controller
 *
 * @author IMS
 * @date 2024-12-11
 */
@RestController
@RequestMapping("/doc/deductionstat")
public class TInDeductionStatController extends BaseController
{
    @Autowired
    private ITInDeductionStatService tInDeductionStatService;

    /**
     * 查询统计表列表
     */
    @PreAuthorize("@ss.hasPermi('doc:deductionstat:list')")
    @GetMapping("/list")
    public TableDataInfo list(TInDeductionStat tInDeductionStat)
    {
        startPage();
        List<TInDeductionStat> list = tInDeductionStatService.selectTInDeductionStatList(tInDeductionStat);
        return getDataTable(list);
    }

    /**
     * 导出统计表列表
     */
    @PreAuthorize("@ss.hasPermi('doc:deductionstat:export')")
    @Log(title = "统计表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TInDeductionStat tInDeductionStat)
    {
        List<TInDeductionStat> list = tInDeductionStatService.selectTInDeductionStatList(tInDeductionStat);
        ExcelUtil<TInDeductionStat> util = new ExcelUtil<TInDeductionStat>(TInDeductionStat.class);
        util.exportExcel(response, list, "统计表数据");
    }

    /**
     * 获取统计表详细信息
     */
    @PreAuthorize("@ss.hasPermi('doc:deductionstat:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(tInDeductionStatService.selectTInDeductionStatById(id));
    }

    /**
     * 统计表生成申请
     */
    @PreAuthorize("@ss.hasPermi('doc:deductionstat:createapply')")
    @GetMapping(value = "/createApply/{id}")
    public AjaxResult createApply(@PathVariable("id") Long id)
    {
        try {
            TInDeductionStat tInDeductionStat = tInDeductionStatService.selectTInDeductionStatById(id);
            tInDeductionStatService.applyTInDeductionStat(tInDeductionStat.getStatId());
        } catch (Exception e) {
            return error("统计表生成申请失败");
        }
        return success();
    }

    /**
     * 统计表撤销申请
     */
    @PreAuthorize("@ss.hasPermi('doc:deductionstat:cancelapply')")
    @GetMapping(value = "/cancelApply/{id}")
    public AjaxResult cancelApply(@PathVariable("id") Long id)
    {
        try {
            TInDeductionStat tInDeductionStat = tInDeductionStatService.selectTInDeductionStatById(id);
            tInDeductionStatService.applyTInDeductionStatCancel(tInDeductionStat.getStatId());
        } catch (Exception e) {
            return error("统计表撤销申请失败");
        }
        return success();
    }

    /**
     * 统计表确认申请
     */
    @PreAuthorize("@ss.hasPermi('doc:deductionstat:confirmapply')")
    @GetMapping(value = "/confirmApply/{id}")
    public AjaxResult confirmApply(@PathVariable("id") Long id)
    {
        try {
            TInDeductionStat tInDeductionStat = tInDeductionStatService.selectTInDeductionStatById(id);
            tInDeductionStatService.applyConfirmTInDeductionStat(tInDeductionStat.getStatId());
        } catch (Exception e) {
            return error("统计表确认签名申请失败");
        }
        return success();
    }

    /**
     * 统计表确认申请
     */
    @PreAuthorize("@ss.hasPermi('doc:deductionstat:confirmcancelapply')")
    @GetMapping(value = "/confirmCancelApply/{id}")
    public AjaxResult confirmCancelApply(@PathVariable("id") Long id)
    {
        try {
            TInDeductionStat tInDeductionStat = tInDeductionStatService.selectTInDeductionStatById(id);
            tInDeductionStatService.applyConfirmTInDeductionStatCancel(tInDeductionStat.getStatId());
        } catch (Exception e) {
            return error("统计表确认签名申请失败");
        }
        return success();
    }

    /**
     * 统计表结果同步
     */
//    @PreAuthorize("@ss.hasPermi('doc:deductionstat:sync')")
    @GetMapping(value = "/sync/{id}")
    public AjaxResult sync(@PathVariable("id") Long id)
    {
        try {
            tInDeductionStatService.syncDeductionStat(id);
        } catch (Exception e) {
            e.printStackTrace();
            return error("统计表确认签名申请失败");
        }
        return success();
    }

    /**
     * 获取当前统计表详细信息
     */
    @PreAuthorize("@ss.hasPermi('doc:deductionstat:query')")
    @GetMapping(value = "/getCurrentDeductionstat")
    public AjaxResult getCurrentDeductionstat()
    {
        return success(tInDeductionStatService.selectCurrentDeductionstat());
    }

    /**
     * 新增统计表
     */
    @PreAuthorize("@ss.hasPermi('doc:deductionstat:add')")
    @Log(title = "统计表", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TInDeductionStat tInDeductionStat)
    {
        return toAjax(tInDeductionStatService.insertTInDeductionStat(tInDeductionStat));
    }

    /**
     * 修改统计表
     */
    @PreAuthorize("@ss.hasPermi('doc:deductionstat:edit')")
    @Log(title = "统计表", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TInDeductionStat tInDeductionStat)
    {
        return toAjax(tInDeductionStatService.updateTInDeductionStat(tInDeductionStat));
    }
}
